package com.custom.tab;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ViewAnimator;
import android.widget.LinearLayout.LayoutParams;

public abstract class AbstractTabActivity extends Activity implements
		OnSharedPreferenceChangeListener {

	// public abstract SharedPreferences getSharedPreferences();
	// public abstract void sendBroadcastMessage(String key,String message);
	// public abstract void startActivityForResult(Intent intent, int
	// requestCode,
	// AbstractViewController avc);
	private SharedPreferences sharedPreferences;
	private View mainView;
	private ViewAnimator tabContentConatiner;
	private LinearLayout tabHeaderPane;
	// private View tabHeaderContainer;
	private List<View> tabHeaderList = new ArrayList<View>();
	private List<ViewAnimator> tabContents = new ArrayList<ViewAnimator>();
	private SparseArray<AbstractTabRootManager> tabRootManagers = new SparseArray<AbstractTabRootManager>();
	private SparseArray<AbstractViewController> avcs = new SparseArray<AbstractViewController>();
	private LayoutInflater layoutInflater;

	private int[] tabHeaderIds = null;
	private int currentSelectedTab = -1;

	public void onCreate(Bundle savedInstanceState, int[] tabHeaderIds,
			int mainLayoutId, int tabHeaderPaneId, int tabContentPaneId,boolean addTabs) {
		onCreate(savedInstanceState, tabHeaderIds,
			mainLayoutId,tabHeaderPaneId, tabContentPaneId,null,null,addTabs);
	}
	
	public void onCreate(Bundle savedInstanceState, int[] tabHeaderIds,
			int mainLayoutId, int tabHeaderPaneId, int tabContentPaneId,String typefaceName,String typefaceAssetsName,boolean addTabs) {
		try {
			super.onCreate(savedInstanceState);
			this.tabHeaderIds = tabHeaderIds;
			if((typefaceName!=null) && (typefaceAssetsName!= null)){
				FontsOverride.setDefaultFont(this,typefaceName ,
						typefaceAssetsName);
			}
			mainView = getLayoutInflater().inflate(mainLayoutId, null);
			setContentView(mainView);
			tabHeaderPane = (LinearLayout) findViewById(tabHeaderPaneId);
			tabContentConatiner = (ViewAnimator) findViewById(tabContentPaneId);
			sharedPreferences = PreferenceManager
					.getDefaultSharedPreferences(this);
			sharedPreferences.registerOnSharedPreferenceChangeListener(this);
			layoutInflater = LayoutInflater.from(this);
			if(addTabs){
				addTabs();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setTabHeaderPaneVisibility(int visibility) {
		tabHeaderPane.setVisibility(visibility);
	}

	public int getTabHeaderPaneVisibility() {
		return tabHeaderPane.getVisibility();
	}

	public View getTabView(int id) {
		View ret = null;
		try {
			final View tab = findViewById(id);
			tab.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					onTabTapped(v);
				}
			});
			ret = tab;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ret;
	}

	public void addTabs() {
		try {

			for (int i = 0; i < tabHeaderIds.length; i++) {
				View view = getTabView(tabHeaderIds[i]);
				tabHeaderList.add(view);
				ViewAnimator viewAnimator = new ViewAnimator(this);
				android.view.ViewGroup.LayoutParams lParams2 = new android.view.ViewGroup.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
				tabContents.add(viewAnimator);
				tabContentConatiner.addView(viewAnimator, lParams2);
			}
			
			if ((tabHeaderList.size() > 0)
					&& (tabHeaderList.size() == tabHeaderIds.length)) {
				for (int i = 0; i < tabHeaderList.size(); i++) {
					tabHeaderList.get(i).setSelected(false);
				}
			}
			onTabTapped(tabHeaderList.get(0));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setSelectedTab(int index) {
		try {
			if ((index < tabHeaderList.size()) && (index >= 0)) {
				onTabTapped(tabHeaderList.get(index));
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void onTabTapped(View view) {
		try {

			for (int i = 0; i < tabHeaderList.size(); i++) {
				View v = tabHeaderList.get(i);
				if (view.equals(v)) {
					if (currentSelectedTab != i) {
						if (tabContentConatiner.getVisibility() == View.GONE) {
							tabContentConatiner.setVisibility(View.VISIBLE);
						}
						currentSelectedTab = i;
						v.setSelected(true);
						View child = tabContentConatiner.getChildAt(i);
						if ((child != null) && (child instanceof ViewAnimator)) {
							ViewAnimator vChild = (ViewAnimator) child;
							showTabContent(i, vChild);
						}
						tabContentConatiner.setDisplayedChild(i);
					}

				} else {
					v.setSelected(false);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void showTabContent(int index, ViewAnimator child) {
		try {
			AbstractTabRootManager tbManager = tabRootManagers.get(index);
			if (tbManager == null) {

				// tbManager = new HomeTabRootManager(this, layoutInflater,
				// child, index);
				tbManager = getTabRootManager(layoutInflater, index, child);
				tabRootManagers.put(index, tbManager);
			}
			tbManager.onResume();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public abstract AbstractTabRootManager getTabRootManager(
			LayoutInflater layoutInflater, int index, ViewAnimator child);

	public void startActivityForResult(Intent intent, int requestCode,
			AbstractViewController avc) {
		avcs.put(requestCode, avc);
		super.startActivityForResult(intent, requestCode);
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		try {
			for (int i = 0; i < tabRootManagers.size(); i++) {
				tabRootManagers.valueAt(i).onConfigurationChanged(newConfig);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onSharedPreferenceChanged(SharedPreferences sp, final String key) {
		runOnUiThread(new Runnable() {

			public void run() {
				try {
					for (int i = 0; i < tabRootManagers.size(); i++) {
						tabRootManagers.valueAt(i).onSharedPreferenceChanged(
								sharedPreferences, key);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		});

	}

	public void sendBroadcastMessage(String key, String message) {
		try {
			for (int i = 0; i < tabRootManagers.size(); i++) {
				tabRootManagers.valueAt(i).broadcastMessageReceived(key,
						message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		try {

			AbstractViewController avc = avcs.get(requestCode);
			if (avc != null) {
				avc.onActivityResult(requestCode, resultCode, data);
			} else {
				super.onActivityResult(requestCode, resultCode, data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public SharedPreferences getSharedPreferences() {
		return sharedPreferences;
	}

	@Override
	public void onBackPressed() {
		try {
			AbstractTabRootManager tabRootManager = tabRootManagers
					.get(currentSelectedTab);
			if (tabRootManager != null) {
				boolean ret = tabRootManager.onBackPressed();
				if (!ret) {
					super.onBackPressed();
				}
			} else {
				super.onBackPressed();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void hideTabWidget(){
		
	}
	public void showTabWidget(){
		
	}
	public void setCurrentTabIndex(int index){
		
	}
	
	public boolean isLandscape(){
		Configuration configuration =getResources().getConfiguration();
		return (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
	}
	public boolean isPortrait(){
		Configuration configuration =getResources().getConfiguration();
		return (configuration.orientation == Configuration.ORIENTATION_PORTRAIT);
	}
}
